/**
 * Copyright(c): 2017 com.mjduan All rights reserved.
 * 项目名：learn-spring-web
 * 注意：未经作者允许，不得外传
 */
package com.mjduan.project.util.redis.subscribe;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.mjduan.project.util.fastjson.FastjsonSerialization;

/**
 * @author mjduan@yahoo.com mjduan 2018-06-24 14:39
 * @version 1.0
 * @since 1.0
 */
public class CustomMessageListener implements MessageListener {
    private static final StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    private FastjsonSerialization fastjsonSerialization = FastjsonSerialization.getInstance();

    @Override
    public void onMessage(Message message, byte[] bytes) {
        String channel = stringRedisSerializer.deserialize(message.getChannel());
        CustomMessage customMessage = fastjsonSerialization.deserialize(message.getBody(), CustomMessage.class);
        System.out.println("channel:" + channel + "; message:" + customMessage);
    }
}
